package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.lang.TypeReference;
import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.object.pojo.WeChatOperationConfiguration;
import com.tbit.uqbike.service.business.WeChatOperationConfigurationService;
import com.tbit.uqbike.webmanager.dao.core.WeChatOperationConfigurationDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @author jing
 * @date 2020/9/2819:37
 * @Description：微信运营配置
 */
@Service("weChatOperationConfiguration")
//@Transactional
public class WeChatOperationConfigurationServiceImpl implements WeChatOperationConfigurationService {

    @Resource
    private WeChatOperationConfigurationDao weChatOperationConfigurationDao;
    @Resource
    private WeixinPayConfigServiceImpl weixinPayConfigService;

    /**
     * @param wechat
     * @return void
     * @Description:添加或修改运营配置
     * @createDate 2020/9/28 19:56
     */
    @Override
    public void add(WeChatOperationConfiguration wechat) {
        //记录商户变更日志
        Map<String, Object> requestBody = JSON.parseObject(JSON.toJSONString(wechat), new TypeReference<Map<String, Object>>() {
        });
        weixinPayConfigService.addLog(wechat.getAccountId(), 3, wechat.getOpId(), requestBody);

        WeChatOperationConfiguration weChatOperation = weChatOperationConfigurationDao.selectByBrandId(wechat.getAccountId());
        /**存在运营配置即修改，否则进行添加*/
        wechat.setUpdateTime(new Date());
        if (null == weChatOperation) {
            weChatOperationConfigurationDao.insert(wechat);
        } else {
            editWeChat(wechat);
        }

    }

    /**
     * @param brandId
     * @return com.tbit.uqbike.object.pojo.WeChatOperationConfiguration
     * @Description: 根据品牌id获取
     * @createDate 2020/9/28 19:56
     */
    @Override
    public WeChatOperationConfiguration getByBrandId(Integer brandId) {
        return weChatOperationConfigurationDao.selectByBrandId(brandId);
    }

    /**
     * @param wechat
     * @return com.tbit.uqbike.object.pojo.WeChatOperationConfiguration
     * @Description: 修改
     * @createDate 2020/9/28 19:56
     */
    @Override
    public void editWeChat(WeChatOperationConfiguration wechat) {
        weChatOperationConfigurationDao.updateByBrandId(wechat);
    }

    @Override
    public WeChatOperationConfiguration getByAreaId(Integer areaId) {
        return weChatOperationConfigurationDao.selectByAreaId(areaId);
    }

}
